package com.example.baselib.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

object TimeUtils {
    private const val realTimeFormat_from = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSS"
    private const val timeFormat_to = "yyyy-MM-dd HH:mm:ss"
    private const val chartFormat_to = "HH:mm:ss"
    val sdf = SimpleDateFormat("yyyy-MM-dd")
    //实时报警的时间格式化;
    fun timeFormat(time: String?): String? {
        if (time == null) {
            return null
        }
        var formatTime: String? = null
        try {
            val date = SimpleDateFormat(realTimeFormat_from).parse(time)
            val simpleDateFormat = SimpleDateFormat(timeFormat_to)
            formatTime = simpleDateFormat.format(date!!)

        } catch (e: ParseException) {
            e.printStackTrace()
            return time
        }

        return formatTime
    }


    //曲线的时间转化;
    fun chartTimeFormat(time: String): String? {
        var formatTime: String? = null
        try {
            val date = SimpleDateFormat(realTimeFormat_from).parse(time)
            val simpleDateFormat = SimpleDateFormat(chartFormat_to)
            formatTime = simpleDateFormat.format(date!!)

        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return formatTime
    }

    //时间转毫秒;
    fun dateToMillSec(date: String?): Long {
        val simpleDateFormat = SimpleDateFormat(realTimeFormat_from)
        var time1: Long = 0
        try {
            if (null != date && date.isNotEmpty())
                time1 = simpleDateFormat.parse(date)!!.time
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return time1
    }

    /**
     * 毫秒时间转换成字符串,指定格式 默认为"yyyy-MM-dd HH:mm:ss"
     *
     * @param time   时间
     * @param format 时间格式
     */
    @JvmOverloads
    fun dateToString(time: Long, format: String = "yyyy.MM.dd HH:mm"): String {
        val date = Date(time)
        val simpleDateFormat = SimpleDateFormat(format)
        return simpleDateFormat.format(date)
    }


    /**
     *
     * 秒转换为时间字符
     */
    fun secondToString(time:Int):String{
        var num: Int
        var second=time%60
        var minute: Int
        var hour: Int
        var res ="$second 秒"
        num=time-second
        if(num>0){
            num/=60
            minute=num%60
            res= "$minute 分$res"
            num-=minute
            if(num>0){
                hour=num/60
                res="$hour 小时$res"
            }

        }
        return res
    }

    fun TimeAdd(type: Int, startTime: String, addTime: Int): String {
        var endTime: String? = null
        val calendar = Calendar.getInstance()
        try {
            calendar.time = SimpleDateFormat(realTimeFormat_from).parse(startTime)!!
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        calendar.add(type, addTime)
        val simpleDateFormat = SimpleDateFormat(timeFormat_to)
        endTime = simpleDateFormat.format(calendar.time)
        return endTime
    }
}

